<?php
/**
 * Created by coder meng.
 * User: coder meng
 * Date: 2018/4/9 13:46
 */
namespace app\blog\controller;
use cmf\controller\AdminBaseController;
use app\blog\model\CashierModel as CA;
use think\Db;
class PayController extends AdminBaseController
{
    public function index()
    {

    }
    /*
     * 获取登录设置
     * */
    public function session()
    {
        $session=cmf_get_current_user();
        return $session;
    }
    /*
 * 支付类型
 * */
    public function payname(){
        $data=DB::name('pay_name')->order('id desc')->paginate(10);
        $this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch('payname');
    }
    /*
     * 添加
     * */
    public function addname()
    {
        $data=$this->request->post();
        if(!empty($data))
        {
            $state=DB::name('pay_name')->insert($data);
            if($state)
            {
                $this->success('添加成功',url('pay/payname'));
            }else{
                $this->error('添加失败',url('pay/addname'));
            }
        }else{
            return $this->fetch('addname');
        }

    }
    /*
     * 更新
     * */
    public function update()
    {
        $data=$this->request->param();
        $where['id']=$data['id'];
        $array=DB::name('pay_name')->where($where)->find();
        $this->assign('array',$array);
        return  $this->fetch('update');

    }
    public function updates()
    {
        $data=$this->request->post();

        $where['id']=$data['pay_id'];
        $array['pay_name']=$data['pay_name'];
        $array['pay_state']=$data['pay_state'];
        $state=DB::name('pay_name')->where($where)->update($array);
        if($state)
        {
            $this->success('修改成功',url('pay/payname'));
        }else{
            $this->error('修改失败',url('pay/update'));
        }

    }
    /*
     * 删除
     * */
    public function del()
    {
        $id=$this->request->param();
        $where['id']=$id['id'];
        $table=$id['table'];
        $state=DB::name($table)->where($where)->delete();
        if($table=='config')
        {
            if($state)
            {
                $this->success('删除成功',url('pay/integral'));
            }else{
                $this->error('删除失败',url('pay/setintegral'));
            }
        }else{
            if($state)
            {
                $this->success('删除成功',url('pay/payname'));
            }else{
                $this->error('删除失败',url('pay/payname'));
            }
        }

    }
    /*
     * 支付设置
     * */
    public function paytype()
    {
        $post=input('request.');
        $where=array();
        if(!empty($post['shop_id']))
        {
            $where['shop_id']=$post['shop_id'];
        }
        $data=DB::name('paytype')->where($where)->order('id desc')->paginate(10);
        $data->appends($where);
        $this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch('paytype');
    }
    /*
     * 设置支付参数
     * */
    public function setpay()
    {
         $array=$this->request->post();
         if(!empty($array))
         {
             $state=DB::name('paytype')->insert($array);
             if($state)
             {
                 $this->success('修改成功',url('pay/payname'));
             }else{
                 $this->error('修改失败',url('pay/update'));
             }
         }else{
             $payname=DB::name('pay_name')->select();
             $this->assign('payname',$payname);
             return $this->fetch('setpay');
         }
    }
    /*
     * 修改支付是否开启和关闭
     * */
    public function state()
    {
      $array=$this->request->param();
      $where['id']=$array['id'];
      if($array['state']== 1)
      {
          $data['state']=2;
      }else{
          $data['state']=1;
      }
      $paytype=DB::name('paytype')->where($where)->update($data);
        if($paytype)
        {
            $this->success('修改成功',url('pay/paytype'),'',0);
        }else{
            $this->error('修改失败',url('pay/paytype'),'',0);
        }
    }
    /*
     * 积分展示
     * */
    public function integral()
    {
        $post=input('request.');
        $where=array();
        if(!empty($post['storeid']))
        {
            $where['storeid']=$post['storeid'];
        }
        $data=DB::name('config')->where($where)->order('id desc')->select()->toArray();
        $this->assign('data',$data);
        return $this->fetch('integral/integral');
    }
    /*
     * 积分设置
     * */
    public function setintegral()
    {
        $session=$this->session();
        if($_POST)
        {
            $data=$this->request->post();
            $data['time']=time();
            
            $state=DB::name('config')->insert($data);
            if($state)
            {
                $this->success('添加成功',url('pay/integral'));
            }else{
                $this->error('添加失败',url('pay/setintegral'));
            }
        }else{
            $shopid=$session['store_id'];
            $this->assign('shopid',$shopid);
            return $this->fetch('integral/setintegral');
        }
    }
    /*
     * 修改积分
     * */
    public function up()
    {
            if($_POST)
            {
                $data=$this->request->post();
                $data['time']=time();
                $where['id']=$data['id'];
                $state=DB::name('config')->where($where)->update($data);
                if($state)
                {
                    $this->success('修改成功',url('pay/integral'));
                }else{
                    $this->error('修改失败',url('pay/setintegral'));
                }
            }else{
                $array=$this->request->param();
                $where['id']=$array['id'];
                $data=DB::name('config')->where($where)->find();
                $this->assign('data',$data);
                return  $this->fetch('integral/upintegral');
            }

    }
}